package org.demo1;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //class类中用于获取构造方法的方法
        //1.先获取class字节码文件对象
        Class clazz = Class.forName("org.demo1.student");
        //2.获取构造方法
        //(1)
        Constructor[] cons = clazz.getConstructors();
        for (Constructor con:cons) {
            System.out.println(con);
        }

        System.out.println();

        //(2)
        Constructor[] con2 = clazz.getDeclaredConstructors();
        for (Constructor con : con2) {
            System.out.println(con);
        }

        System.out.println();

        //(3)
        Constructor con3 = clazz.getConstructor();
        System.out.println(con3);

        System.out.println();

        //(4)
        Constructor con4 = clazz.getDeclaredConstructor(String.class);
        System.out.println(con4);

        System.out.println();

        Constructor con5 = clazz.getDeclaredConstructor(int.class);
        System.out.println(con5);

        System.out.println();

        Constructor con6 = clazz.getDeclaredConstructor(String.class, int.class);
        System.out.println(con6);
        int modifiers = con6.getModifiers();//获取权限修饰符
        System.out.println(modifiers);
        Parameter[] parameters = con6.getParameters();//获取构造方法的每个参数
        for (Parameter parameter : parameters) {
            System.out.println(parameter);
        }
        con6.setAccessible(true);//零时取消权限校验（暴力反射）
        student stu = (student) con6.newInstance("张三", 34);
        System.out.println(stu);

    }
}